Skip to main content

Capture dmesg Output

Description

The capture_dmesg function captures the output of the dmesg command, which displays the kernel ring buffer messages, and saves it to a log file. It optionally deletes older logs, keeping only the most recent logs.

Function Signature:

def capture_dmesg(output_folder_path: str, delete_old_logs: bool = False) -> None:

Parameters

  • output_folder_path (str): The folder where the log file will be saved.
  • delete_old_logs (bool): Whether to delete old log files, keeping only the most recent 20 logs. Default is False.

Returns

  • None: The function does not return any value.

Example Usage

capture_dmesg("/path/to/logs", delete_old_logs=True)

Notes

  • The function generates a timestamped filename based on the current date and time (e.g., dmesg_2025-04-09_14-30-25.log).
  • The dmesg command output is saved to the specified log file.
  • If delete_old_logs is set to True, older log files in the specified folder are deleted, retaining only the most recent 20 logs.

Error Handling

  • If any error occurs (e.g., permission issues, failure to run dmesg), the function will print the error message.